home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / tk / xtk01.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-24  |  7.6 KB  |  322 lines

  1. /* $Id: xtk01.c,v 1.6 1994/06/23 22:40:06 mjl Exp $
  2.  * $Log: xtk01.c,v $
  3.  * Revision 1.6  1994/06/23  22:40:06  mjl
  4.  * Fix to get prototype of pltkMain() correct.
  5.  *
  6.  * Revision 1.5  1994/06/16  19:30:14  mjl
  7.  * Changes to use pltkMain() for creating extended wish.  Should be more
  8.  * portable and robust than old method.
  9.  *
  10.  * Revision 1.4  1994/06/09  20:29:54  mjl
  11.  * Minor cleaning up.
  12.  *
  13.  * Revision 1.3  1994/05/26  22:38:07  mjl
  14.  * Added missing CVS Id and Log fields.
  15.  */
  16.  
  17. /* Before including plplot.h you must define TK to get all prototypes */
  18.  
  19. #define TK
  20. #include <plplot.h>
  21. #include <tk.h>
  22. #include <math.h>
  23.  
  24. /*----------------------------------------------------------------------*\
  25.  * main --
  26.  *
  27.  * Just a stub routine to call pltkMain.  The latter is nice to have
  28.  * when building extended wishes, since then you don't have to rely on
  29.  * sucking the Tk main out of libtk (which doesn't work correctly on all
  30.  * systems/compilers/linkers/etc).  Hopefully in the future Tk will
  31.  * supply a sufficiently capable tkMain() type function that can be used
  32.  * instead. 
  33. \*----------------------------------------------------------------------*/
  34.  
  35. int
  36. main(int argc, char **argv)
  37. {
  38.     exit(pltkMain(argc, argv));
  39. }
  40.  
  41. /*
  42.  *----------------------------------------------------------------------
  43.  *
  44.  * Tcl_AppInit --
  45.  *
  46.  *    This procedure performs application-specific initialization.
  47.  *    Most applications, especially those that incorporate additional
  48.  *    packages, will have their own version of this procedure.
  49.  *
  50.  * Results:
  51.  *    Returns a standard Tcl completion code, and leaves an error
  52.  *    message in interp->result if an error occurs.
  53.  *
  54.  * Side effects:
  55.  *    Depends on the startup script.
  56.  *
  57.  * Taken from tkAppInit.c --
  58.  *
  59.  * Copyright (c) 1993 The Regents of the University of California.
  60.  * All rights reserved.
  61.  *
  62.  * Permission is hereby granted, without written agreement and without
  63.  * license or royalty fees, to use, copy, modify, and distribute this
  64.  * software and its documentation for any purpose, provided that the
  65.  * above copyright notice and the following two paragraphs appear in
  66.  * all copies of this software.
  67.  * 
  68.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  69.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  70.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  71.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72.  *
  73.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  74.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  75.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  76.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  77.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  78.  *----------------------------------------------------------------------
  79.  */
  80.  
  81. int   myplotCmd        (ClientData, Tcl_Interp *, int, char **);
  82.  
  83. int
  84. Tcl_AppInit(interp)
  85.     Tcl_Interp *interp;        /* Interpreter for application. */
  86. {
  87.     Tk_Window main;
  88.  
  89.     main = Tk_MainWindow(interp);
  90.  
  91.     /*
  92.      * Call the init procedures for included packages.  Each call should
  93.      * look like this:
  94.      *
  95.      * if (Mod_Init(interp) == TCL_ERROR) {
  96.      *     return TCL_ERROR;
  97.      * }
  98.      *
  99.      * where "Mod" is the name of the module.
  100.      */
  101.  
  102.     if (Tcl_Init(interp) == TCL_ERROR) {
  103.     return TCL_ERROR;
  104.     }
  105.     if (Tk_Init(interp) == TCL_ERROR) {
  106.     return TCL_ERROR;
  107.     }
  108.     if (Pltk_Init(interp) == TCL_ERROR) {
  109.     return TCL_ERROR;
  110.     }
  111.  
  112.     /*
  113.      * Call Tcl_CreateCommand for application-specific commands, if
  114.      * they weren't already created by the init procedures called above.
  115.      */
  116.  
  117.     Tcl_CreateCommand(interp, "myplot", myplotCmd,
  118.                       (ClientData) main, (void (*)(ClientData)) NULL);
  119.  
  120.     return TCL_OK;
  121. }
  122.  
  123. void myplot1();
  124. void myplot2();
  125. void myplot3();
  126. void myplot4();
  127.  
  128. /* Plots several simple functions */
  129. /* Note the compiler should automatically convert all non-pointer arguments
  130.    to satisfy the prototype, but some have problems with constants. */
  131.  
  132. static PLFLT xs[6] =
  133. {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
  134. static PLFLT ys[6] =
  135. {1.0, 4.0, 9.0, 16.0, 25.0, 36.0};
  136. static PLFLT x[101], y[101];
  137. static PLFLT xscale, yscale, xoff, yoff, xs1[6], ys1[6];
  138. static PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
  139.  
  140. void plot1(void);
  141. void plot2(void);
  142. void plot3(void);
  143.  
  144. void myplot1()
  145. {
  146. /* Set up the data */
  147. /* Original case */
  148.  
  149.     xscale = 6.;
  150.     yscale = 1.;
  151.     xoff = 0.;
  152.     yoff = 0.;
  153.  
  154. /* Do a plot */
  155.  
  156.     plot1();
  157. }
  158.  
  159. void myplot2()
  160. {
  161.     PLINT digmax;
  162.  
  163. /* Set up the data */
  164.  
  165.     xscale = 1.;
  166.     yscale = 0.0014;
  167.     yoff = 0.0185;
  168.  
  169. /* Do a plot */
  170.  
  171.     digmax = 5;
  172.     plsyax(digmax, 0);
  173.     plot1();
  174. }
  175.  
  176. void myplot3()
  177. {
  178.     plot2();
  179. }
  180.  
  181. void myplot4()
  182. {
  183.     plot3();
  184. }
  185.  
  186.  /* =============================================================== */
  187.  
  188. void
  189. plot1(void)
  190. {
  191.     int i;
  192.     PLFLT xmin, xmax, ymin, ymax;
  193.  
  194.     for (i = 0; i < 60; i++) {
  195.     x[i] = xoff + xscale * (i + 1) / 60.0;
  196.     y[i] = yoff + yscale * pow(x[i], 2.);
  197.     }
  198.  
  199.     xmin = x[0];
  200.     xmax = x[59];
  201.     ymin = y[0];
  202.     ymax = y[59];
  203.  
  204.     for (i = 0; i < 6; i++) {
  205.     xs1[i] = x[i * 10 + 3];
  206.     ys1[i] = y[i * 10 + 3];
  207.     }
  208.  
  209. /* Set up the viewport and window using PLENV. The range in X is */
  210. /* 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are */
  211. /* scaled separately (just = 0), and we just draw a labelled */
  212. /* box (axis = 0). */
  213.  
  214.     plcol(1);
  215.     plenv(xmin, xmax, ymin, ymax, 0, 0);
  216.     plcol(6);
  217.     pllab("(x)", "(y)", "#frPLPLOT Example 1 - y=x#u2");
  218.  
  219. /* Plot the data points */
  220.  
  221.     plcol(9);
  222.     plpoin(6, xs1, ys1, 9);
  223.  
  224. /* Draw the line through the data */
  225.  
  226.     plcol(4);
  227.     plline(60, x, y);
  228. }
  229.  
  230.  /* =============================================================== */
  231.  
  232. void
  233. plot2(void)
  234. {
  235.     int i;
  236.  
  237. /* Set up the viewport and window using PLENV. The range in X is -2.0 to
  238.    10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
  239.    (just = 0), and we draw a box with axes (axis = 1). */
  240.  
  241.     plcol(1);
  242.     plenv((PLFLT) -2.0, (PLFLT) 10.0, (PLFLT) -0.4, (PLFLT) 1.2, 0, 1);
  243.     plcol(2);
  244.     pllab("(x)", "sin(x)/x", "#frPLPLOT Example 1 - Sinc Function");
  245.  
  246. /* Fill up the arrays */
  247.  
  248.     for (i = 0; i < 100; i++) {
  249.     x[i] = (i - 19.0) / 6.0;
  250.     y[i] = 1.0;
  251.     if (x[i] != 0.0)
  252.         y[i] = sin(x[i]) / x[i];
  253.     }
  254.  
  255. /* Draw the line */
  256.  
  257.     plcol(3);
  258.     plline(100, x, y);
  259.  
  260. }
  261.  
  262.  /* =============================================================== */
  263.  
  264. void
  265. plot3(void)
  266. {
  267.     int i;
  268.  
  269. /* For the final graph we wish to override the default tick intervals, and
  270.    so do not use PLENV */
  271.  
  272.     pladv(0);
  273.  
  274. /* Use standard viewport, and define X range from 0 to 360 degrees, Y range
  275.        from -1.2 to 1.2. */
  276.  
  277.     plvsta();
  278.     plwind((PLFLT) 0.0, (PLFLT) 360.0, (PLFLT) -1.2, (PLFLT) 1.2);
  279.  
  280. /* Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y. */
  281.  
  282.     plcol(1);
  283.     plbox("bcnst", (PLFLT) 60.0, 2, "bcnstv", (PLFLT) 0.2, 2);
  284.  
  285. /* Superimpose a dashed line grid, with 1.5 mm marks and spaces. plstyl
  286.    expects a pointer!! */
  287.  
  288.     plstyl(1, &mark1, &space1);
  289.     plcol(2);
  290.     plbox("g", (PLFLT) 30.0, 0, "g", (PLFLT) 0.2, 0);
  291.     plstyl(0, &mark0, &space0);
  292.  
  293.     plcol(3);
  294.     pllab("Angle (degrees)", "sine", "#frPLPLOT Example 1 - Sine function");
  295.  
  296.     for (i = 0; i < 101; i++) {
  297.     x[i] = 3.6 * i;
  298.     y[i] = sin(x[i] * 3.141592654 / 180.0);
  299.     }
  300.  
  301.     plcol(4);
  302.     plline(101, x, y);
  303. }
  304.  
  305. int   myplotCmd( ClientData cd, Tcl_Interp *interp, int argc, char **argv )
  306. {
  307.     if (!strcmp(argv[1],"1"))
  308.       myplot1();
  309.  
  310.     if (!strcmp(argv[1],"2"))
  311.       myplot2();
  312.  
  313.     if (!strcmp(argv[1],"3"))
  314.       myplot3();
  315.  
  316.     if (!strcmp(argv[1],"4"))
  317.       myplot4();
  318.  
  319.     plflush();
  320.     return TCL_OK;
  321. }
  322.